﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AgileTimeTracker.Client.Wpf.Common;
using AgileTimeTracker.Client.Wpf.Services;
using AgileTimeTracker.Client.Wpf.TimeControlModule;
using AgileTimeTracker.Domain.UserModule;

namespace AgileTimeTracker.Client.Wpf.AccessControlModule
{
    public class ProjectSelectionViewModel : PageViewModel
    {
        private ObservableCollection<UserProject> projects;

        private UserProject selectedProject;

        public ProjectSelectionViewModel()
        {
            this.Title = "Select a Project";
        }

        public ObservableCollection<UserProject> Projects
        {
            get { return this.projects; }
            set { this.OnPropertyChanged(ref this.projects, value); }
        }

        public UserProject SelectedProject
        {
            get { return this.selectedProject; }
            set { this.OnPropertyChanged(ref this.selectedProject, value); }
        }

        #region Commands        

        public RelayCommand EditProjectCommand { get; set; }

        public RelayCommand AddNewProjectCommand { get; set; }

        public RelayCommand SelectProjectCommand { get; set; }

        #endregion // Commands

        #region Initialize

        protected override void InititalizeCommands()
        {
            base.InititalizeCommands();
            this.EditProjectCommand = new RelayCommand(this.EditProject, this.CanEditProject);
            this.AddNewProjectCommand = new RelayCommand(this.AddNewProject);
            this.SelectProjectCommand = new RelayCommand(this.SelectProject, this.CanSelectProject);
        }

        protected override void Load()
        {
            base.Load();
            this.Projects = new ObservableCollection<UserProject>(MainWindowViewModel.Instance.CurrentUser.Projects);
        }

        #endregion // Initialize

        #region Property Change

        protected override void HandlePropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.HandlePropertyChanged(sender, e);
            switch(e.PropertyName)
            {
                case "SelectedProject":
                    this.SelectProjectCommand.RaiseCanExecuteChanged();
                    this.EditProjectCommand.RaiseCanExecuteChanged();
                    break;
            }
        }

        #endregion // Property Change

        #region Command Methods

        private void EditProject()
        {
            MainWindowViewModel.Instance.CurrentView = new EditProjectViewModel(this.selectedProject);
        }

        private bool CanEditProject()
        {
            return this.selectedProject != null;
        }

        private void AddNewProject()
        {
            MainWindowViewModel.Instance.CurrentView = new EditProjectViewModel();
        }

        private void SelectProject()
        {
            MainWindowViewModel.Instance.CurrentProject = this.selectedProject;
            MainWindowViewModel.Instance.CurrentView = new WorkViewModel();
        }

        private bool CanSelectProject()
        {
            return this.selectedProject != null;
        }

        #endregion // Command Methods
    }
}
